//
//  BaseTableViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/7/12.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

class BaseTableViewController: UITableViewController,NetProtocol {

    func reset()->Void{
        
    }
    
    @objc func canceledit()->Void{
        self.view.endEditing(true)
    }
    
    @objc func endedit()->Void{
        self.view.endEditing(true)
    }
    
    var names: Array<String>{
        return []
    }
    
    func failBlock(for name: String) -> NetFail {
        
        func fail(error: Error)->Void{
            self.alert(error.localizedDescription)
        }
        
        return fail
    }
    
    func success(for name: String) -> NetSuccess {
        func success(json: JSON)->Void{
            
        }
        
        return success
    }
    
    func param(for name: String) -> Dictionary<String, Any> {
        return [:]
    }
    
    func path(for name: String) -> String {
        return ""
    }
    
    func load(with name: String) {
        let result = strategy(for: name)
        if result.0 == false {
            if result.2 != nil {
                result.2!();
            }else{
                self.alert(result.1)
            }
            return
        }
        self.beginLoading()
        switch self.method(for: name) {
        case .get:
            NetManager.get(self.path(for: name), self.param(for: name), suc: {(source) in
                self.endLoading()
                self.success(for: name)(source)
            }, fail: {(error) in
                self.endLoading()
                self.failBlock(for: name)(error)
            })
            break
        default:
            print(self.param(for: name))
            NetManager.post(self.path(for: name), self.param(for: name), suc: {(source) in
                self.endLoading()
                self.success(for: name)(source)
            }, fail: {(error) in
                self.endLoading()
                self.failBlock(for: name)(error)
            })
        }
    }
    
    func method(for name: String) -> NetMethod {
        return .get
    }
    
    func strategy(for name: String) -> (Bool, String?, (() -> Void)?) {
        return (true,nil,nil)
    }
    
    @objc func popobjc()->Void{
        self.view.endEditing(true)
        self.pop()
    }
    
    var toolbarview = UIView()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.hidesBackButton = true
        if self.navigationController != nil {
            if let index = self.navigationController?.children.firstIndex(of: self) {
                if index > 0 {
                    let back = UIBarButtonItem.init(image: UIImage.init(named: "icon_nav_whiteback")!.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(popobjc))
                    self.navigationItem.leftBarButtonItem = back
                } else {
                    self.navigationItem.leftBarButtonItem = nil
                }
            }
        }
        toolbarview.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: UIScreen.main.bounds.size.width, height: 40))
        toolbarview.layer.borderWidth = 0.5
        toolbarview.layer.borderColor = UIColor.hex("999999").cgColor
        let cancelButton = UIButton.init(type: .system)
        cancelButton.setTitleColor(UIColor.black, for: .normal)
        cancelButton.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 100, height: 40))
        cancelButton.setTitle("取消", for: .normal)
        cancelButton.addTarget(self, action: #selector(canceledit), for: .touchUpInside)
        toolbarview.addSubview(cancelButton)
        let sureButton = UIButton.init(type: .system)
        sureButton.setTitleColor(UIColor.black, for: .normal)
        sureButton.frame = CGRect.init(origin: CGPoint.init(x: UIScreen.main.bounds.size.width - 100, y: 0), size: CGSize.init(width: 100, height: 40))
        sureButton.setTitle("确定", for: .normal)
        sureButton.addTarget(self, action: #selector(endedit), for: .touchUpInside)
        toolbarview.addSubview(sureButton)
        toolbarview.backgroundColor = UIColor.white
        self.tableView.estimatedRowHeight = 44.0;
        
        self.tableView.backgroundColor = UIColor.dftBack
        
        self.tableView.rowHeight = UITableView.automaticDimension;
        
        self.tableView.backgroundColor = UIColor.dftBack
        
        if self.tableView.tableFooterView == nil {
            
            self.tableView.tableFooterView = UIView()
            
        }
        
    }
    
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func xy_noDataViewImage() -> UIImage {
        return UIImage(named: "img_no_data")!
    }
    
    func xy_noDataViewMessage() -> String {
        return "暂无数据"
    }
    
    func xy_noMoreDataTitle() -> String {
        return "没有更多数据"
    }

}
